package gqz.testdemo.DragListViewGroup;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import gqz.testdemo.R;

/**
 * Created by Administrator on 2018/8/21.
 */

public class DragListViewActivity extends Activity {

    private String TAG = "DragListViewActivity-->";

    private AbsoluteLayout abs;
    private ListView lv1, lv2;
    private TextView tv;

    private ListViewAdapter adapter, adapter2;

    private boolean showDragText = false;
    private int dragItemId = -1;
    private ArrayList<String> data = new ArrayList<>();
    private ArrayList<String> lv2Data = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag);

        abs = findViewById(R.id.drag_root);
        lv1 = findViewById(R.id.drag_lv1);
        lv2 = findViewById(R.id.drag_lv2);
        lv1.setOnTouchListener(ontouch);
        lv2.setOnTouchListener(ontouch);
        tv = findViewById(R.id.drag_tv);
        tv.setText("中华小曲库");
        tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
        tv.setBackgroundColor(Color.parseColor("#10000000"));
        tv.setVisibility(View.GONE);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int w = dm.widthPixels;
        ViewGroup.LayoutParams param = lv1.getLayoutParams();
        param.width = w / 2;
        lv1.setLayoutParams(param);
        ViewGroup.LayoutParams param2 = lv2.getLayoutParams();
        param2.width = w / 2;
        lv2.setLayoutParams(param2);
        lv2.setX(w / 2);
        Log.i(TAG, "onCreate: lv1 xy" + lv1.getX() + "*" + lv1.getY());
        Log.i(TAG, "onCreate: lv2 xy" + lv2.getX() + "*" + lv2.getY());

        initData();
        adapter = new ListViewAdapter(this, data);
        lv1.setAdapter(adapter);
        lv1.setOnItemLongClickListener(itemLongClick);

        adapter2 = new ListViewAdapter(this, lv2Data);
        lv2.setAdapter(adapter2);
        lv2.setOnItemLongClickListener(itemLongClick);

    }

    private void initData() {
        if (data.size() > 0) data.clear();
        for (int i = 0; i < 20; i++) {
            String str = "Item " + (i + 1);
            data.add(str);
        }
    }

    private boolean pointInView(View v, PointF p) {
        float x = p.x;
        float y = p.y;
        int left = (int) v.getX();
        int top = (int) v.getY();
        int right = left + v.getRight();
        int bottom = top + v.getBottom();
        Log.d(TAG, "pointInView: " + left + " " + top + " " + right + " " + bottom);
        if (x >= left && x <= right && y >= top && y <= bottom)
            return true;
        return false;
    }

    String fromView = "none";
    View.OnTouchListener ontouch = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x = event.getX() + v.getX() - tv.getRight() - 50;
            float y = event.getY() + v.getY() - tv.getBottom();
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (showDragText) {
                        tv.setX(x);
                        tv.setY(y);
                        tv.setVisibility(View.VISIBLE);
                    }
                    if (pointInView(lv1, new PointF(x, y)))
                        fromView = "lv1";
                    else if (pointInView(lv2, new PointF(x, y)))
                        fromView = "lv2";
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (showDragText) {
                        if (tv.getVisibility() == View.GONE)
                            tv.setVisibility(View.VISIBLE);
                        tv.setX(x);
                        tv.setY(y);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if (showDragText) {
                        tv.setVisibility(View.GONE);
                        if (pointInView(lv1, new PointF(x, y))) {
                            Log.i(TAG, "onTouch: from " + fromView + " point in lv1");
                            if (fromView.equals("lv2")) {
                                lv2Data.remove(dragItemId);
                                adapter2.notifyDataSetChanged();
                            }
                        } else if (pointInView(lv2, new PointF(x, y))) {
                            Log.i(TAG, "onTouch: from " + fromView + " point in lv2");
                            if (fromView.equals("lv1")) {
                                lv2Data.add(data.get(dragItemId));
                                adapter2.notifyDataSetChanged();
                            }
                        }
                        fromView = "none";
                        showDragText = false;
                    }
                    break;
            }
            return false;
        }
    };

    AdapterView.OnItemLongClickListener itemLongClick = new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            String txt = "";
            switch (parent.getId()) {
                case R.id.drag_lv1:
                    txt = data.get(position);
                    break;
                case R.id.drag_lv2:
                    txt = lv2Data.get(position);
                    break;
            }
            Toast.makeText(DragListViewActivity.this, txt, Toast.LENGTH_SHORT).show();
            showDragText = true;
            dragItemId = position;
            tv.setText(txt);
            return false;
        }
    };

    class ListViewAdapter extends BaseAdapter {

        private Context context;
        private ArrayList<String> list = new ArrayList<>();

        public ListViewAdapter(Context context, ArrayList<String> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_text, null);
                holder.tv = convertView.findViewById(R.id.item_text_tv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tv.setText(list.get(position));

            return convertView;
        }

        class ViewHolder {
            TextView tv;
        }
    }

}
